<template>
  <div class="jurisdiction">
    <!-- 搜索区域 -->
    <el-form ref="dataForm" class="jurisdiction_nav">
      <el-button
        v-if="button1"
        class="add"
        type="primary"
        size="medium"
        @click="creat"
      >添加一条</el-button>
    </el-form>
    <el-table :data="roles" style="width: 100%" :border="true" :highlight-current-row="true">
      <el-table-column label="角色ID" align="center" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.role_id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="角色名字" align="center" width="140">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <el-tag size="medium">{{ scope.row.role_name }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" width="250">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <el-tag size="medium">{{ scope.row.creater }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="注册时间" align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <span>{{ scope.row.create_time }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button v-show="button2" size="medium" type="success" @click="update(scope.row)">编辑</el-button>

          <el-button v-if="button3" size="medium" type="danger" @click="del(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 确认删除 -->
    <el-dialog :visible.sync="deleteDataFrom" width="30%">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="70px"
        style="width: 400px; margin-left:50px;"
      >
        <span>确定删除这一条吗？</span>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="deleteDataFrom = false">取消</el-button>
        <el-button type="primary" @click="deleteData">确定删除</el-button>
      </div>
    </el-dialog>
    <!-- 分页器 -->
    <el-col :span="24" class="toolbar" style="text-align: center;">
      <el-pagination
        background
        :current-page="currentPage"
        :page-count="pageNum"
        :page-size="pagesize"
        style="margin-top:30px;margin-bottom:30px"
        layout="total, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-col>
  </div>
</template>
<script>
import request from '@/utils/request'
import { getToken, getAccount, getButtonpermsStr } from '@/utils/auth'
import { Button } from 'element-ui'
export default {
  data() {
    return {
      roles: [],
      currentPage: 1, // 初始页
      pagesize: 10, // 当前页面内的列表行数
      pageNum: 1, // 总数页数，
      total: 0,
      temp: {},
      deleteDataFrom: false,
      rules: {},
      button1: false,
      button2: false,
      button3: false,
      buttonpermsStr: getButtonpermsStr()
    }
  },
  mounted() {
    if (this.buttonpermsStr !== undefined) {
      if (this.buttonpermsStr.indexOf(13) > -1) {
        this.button1 = true
      }
      if (this.buttonpermsStr.indexOf(14) > -1) {
        this.button2 = true
      }
      if (this.buttonpermsStr.indexOf(166) > -1) {
        this.button3 = true
      }
    }

    this.getList()
  },
  methods: {
    // 添加一条
    creat() {
      this.$router.push({
        path: '/admin/jurisdiction/ejurisdiction',
        query: {
          id: '',
          name: ''
        }
      })
    },
    // 编辑
    update(row) {
      this.$router.push({
        path: '/admin/jurisdiction/ejurisdiction',
        query: {
          id: row.role_id,
          name: row.role_name
        }
      })
    },
    // 删除
    del(row) {
      this.temp = {
        role_id: row.role_id
      }
      this.deleteDataFrom = true
    },
    // 确认删除
    deleteData() {
      return request({
        url: '/v3/admin/role/delete',
        method: 'post',
        headers: {
          account_type: getAccount()
        },
        data: this.temp
      }).then(res => {
        this.deleteDataFrom = false
        this.$router.go(0) // 刷新页面
      })
    },
    // 获取列表
    getList() {
      return request({
        url: '/v3/admin/role/list',
        method: 'post',
        headers: {
          account_type: getAccount()
        },
        data: {}
      }).then(res => {
        this.roles = res.roles
        this.currentPage = res.current_page
        this.pageNum = res.pages
        this.total = res.total_record
      })
    },
    // 当前页
    handleSizeChange: function(pagesize) {
      this.pagesize = pagesize
      this.getList()
    },
    // 换页：更新列表数据
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
      this.getList()
    }
  }
}
</script>
<style lang = "scss" scoped>
.jurisdiction {
  margin-top: 55px;
  overflow: hidden;
}
.jurisdiction_nav {
margin: 10px;
}
</style>
